<?php
namespace app\admin\controller;
use think\Controller;
use think\Loader;
//跨模块调用控制器
use app\apipay\MicroPay;
use app\apipay\JsApiPay;
use app\apipay\NativePay;
use app\apipay\OrderQuery;
use app\apipay\Refund;
use app\apipay\RefundQuery;
use app\apipay\Download;
use app\apipay\NativeNotifyCallBack;

class Wtpay extends Common
{
	
	public function jsapi(){
		//$jsapi = controller('JsApiPay', 'controller\apipay');
		$jsapi = new JsApiPay();
		$result = $jsapi->start();	
		print_r($result);exit;	
	}
	public function micropay(){
		//$micropay = controller('MicroPay', 'controller\apipay');
		$micropay = new MicroPay();
		$result = $micropay->start();
		print_r($result);exit;
	}
	public function native($productId=9999){
		//$native = controller('apipay/NativePay', 'apipay');//跨模块分层控制器调用
		//$native = controller('NativePay', 'controller\apipay');//分层控制器调用
		$native = new NativePay();
		//$result = $native->GetPrePayUrl($productId);//模式一
		$result = $native->GetPayUrl($productId);//模式二

		$url = urlencode($result); //二维码内容 
		$url = urldecode($url);
		if(substr($url, 0, 6) == "weixin"){
			createQrcodeOriginal($url);
		}else{
			header('HTTP/1.1 404 Not Found');
		} 
	}
	public function orderquery(){
		if((isset($_REQUEST["transaction_id"]) && $_REQUEST["transaction_id"] != "" 
			&& !preg_match("/^[0-9a-zA-Z]{10,64}$/i", $_REQUEST["transaction_id"], $matches)) 
		|| (isset($_REQUEST["out_trade_no"]) && $_REQUEST["out_trade_no"] != "" 
			&& !preg_match("/^[0-9a-zA-Z]{10,64}$/i", $_REQUEST["out_trade_no"], $matches))){
			$transaction_id = $_REQUEST["transaction_id"];
			$out_trade_no = $_REQUEST["out_trade_no"];
			//$orderquery = controller('OrderQuery', 'controller\apipay');
			$orderquery = new OrderQuery();
			$result = $orderquery->query($transaction_id,$out_trade_no);
			print_r($result);
			exit();
		}
		$this->success('请输入订单号');
		exit();

	}
	public function refund(){
		if((isset($_REQUEST["transaction_id"]) && $_REQUEST["transaction_id"] != "" 
			&& !preg_match("/^[0-9a-zA-Z]{10,64}$/i", $_REQUEST["transaction_id"], $matches))
		|| (isset($_REQUEST["out_trade_no"]) && $_REQUEST["out_trade_no"]!="" 
			&& !preg_match("/^[0-9a-zA-Z]{10,64}$/i", $_REQUEST["out_trade_no"], $matches))
		|| (isset($_REQUEST["total_fee"]) && $_REQUEST["total_fee"] != "" 
			&& !preg_match("/^[0-9]{0,10}$/i", $_REQUEST["total_fee"], $matches))
		|| (isset($_REQUEST["refund_fee"]) && $_REQUEST["refund_fee"] != "" 
			&& !preg_match("/^[0-9]{0,10}$/i", $_REQUEST["refund_fee"], $matches)))
		{
			$transaction_id = $_REQUEST["transaction_id"];
			$out_trade_no = $_REQUEST["out_trade_no"];
			$total_fee = $_REQUEST['total_fee'];
			$refund_fee = $_REQUEST['refund_fee'];
			//$refund = controller('Refund', 'controller\apipay');
			$refund = new Refund();
			$result = $refund->retreat($transaction_id,$out_trade_no,$total_fee,$refund_fee);
			print_r($result);
			exit();
		}
		$this->success('请输入订单号');
		exit();
	}
	public function refundquery(){
		if( (isset($_REQUEST["transaction_id"]) && $_REQUEST["transaction_id"] != "" 
		&& !preg_match("/^[0-9a-zA-Z]{10,64}$/i", $_REQUEST["transaction_id"], $matches))
		|| (isset($_REQUEST["out_trade_no"]) && $_REQUEST["out_trade_no"] != ""
			&& !preg_match("/^[0-9a-zA-Z]{10,64}$/i", $_REQUEST["out_trade_no"], $matches))
		|| (isset($_REQUEST["out_refund_no"]) && $_REQUEST["out_refund_no"] != "" 
			&& !preg_match("/^[0-9a-zA-Z]{10,64}$/i", $_REQUEST["out_refund_no"], $matches))
		|| (isset($_REQUEST["refund_id"]) && $_REQUEST["refund_id"] != "" 
			&& !preg_match("/^[0-9a-zA-Z]{10,64}$/i", $_REQUEST["refund_id"], $matches)))
		{
			$transaction_id = $_REQUEST["transaction_id"];
			$out_trade_no = $_REQUEST["out_trade_no"];
			$out_refund_no = $_REQUEST['out_refund_no'];
			$refund_id = $_REQUEST['refund_id'];
			//$refundquery = controller('RefundQuery', 'controller\apipay');
			$refundquery = new RefundQuery();
			$result = $refundquery->check($transaction_id,$out_trade_no,$out_refund_no,$refund_id);
			print_r($result);
			exit();
		}
		$this->success('请输入订单号');
	}
	public function download(){
			 $_REQUEST["bill_date"] = time();
			 $_REQUEST["bill_type"] = 'ALL';
		if((isset($_REQUEST["bill_date"]) && !preg_match("/^[0-9-]{6,64}$/i", $_REQUEST["bill_date"], $matches))
			|| (isset($_REQUEST["bill_type"]) && !preg_match("/^[A-Z]{1,64}$/i", $_REQUEST["bill_date"], $matches)))
		{
			$bill_date = $_REQUEST["bill_date"];
			$bill_type = $_REQUEST["bill_type"]?$_REQUEST["bill_type"]:'ALL';
			//$download = controller('Download', 'controller\apipay');
			$download = new Download();
			$result = $download->implement($bill_date,$bill_type);
			print_r($result);
			exit();
		}
		$this->success('请选择时间');
	}
	//模式一扫码支付回调
	public function native_order()
	{
		$openId = 'oBrSLw1y5hxm7Mrdrvh0adr1oXtU';
		$product_id = 9999;
		$body='测试';
		$tattach = '测试';
		$total_fee = 1;
		$goods_tag='测试';
		$out_trade_no = 9999;
		$native_order = new NativeNotifyCallBack();
		$result = $native_order->unifiedorder($openId, $product_id,$body,$tattach,$total_fee,$goods_tag,$out_trade_no);
	}
	
}